//
//  LZMyRestaurantInfoTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyRestaurantInfoTableViewCell.h"
#import "Masonry.h"
#import "LZMerchantInfoModel.h"

@interface LZMyRestaurantInfoTableViewCell()

@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * subLabel;
@property (nonatomic, strong) UIImageView * image;
@property (nonatomic, strong) UIView * line;
@property (nonatomic, strong) UIImageView * accessoryImageView;

@end

@implementation LZMyRestaurantInfoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andType:(LZ_MyRESTAURANT_CELLTYPE)type
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.type = type;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subLabel];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.accessoryImageView];
    
    if (self.type == LZ_MyRESTAURANT_CELLTYPE_IMAGE) {
        [self.contentView addSubview:self.image];
    }
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    
    if (self.type == LZ_MyRESTAURANT_CELLTYPE_SINGLE) {
        
        self.subLabel.numberOfLines    = 1;
        
        [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSelf.centerY);
            make.leading.mas_equalTo(10);
        }];
        
        [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSelf.centerY);
            make.leading.mas_equalTo(weakSelf.titleLabel.mas_trailing).with.offset(10);
            make.trailing.mas_lessThanOrEqualTo(-10);
        }];
    }
    else if (self.type == LZ_MyRESTAURANT_CELLTYPE_DOUBLE){
        
        self.subLabel.numberOfLines    = 0;
        
        [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo (15);
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(weakSelf.accessoryImageView.mas_leading).with.offset(-10);
            make.height.mas_equalTo(20);
        }];
        
        [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(10);
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.bottom.mas_equalTo(-10);
        }];
    }
    else if (self.type == LZ_MyRESTAURANT_CELLTYPE_IMAGE) {
        
        self.subLabel.numberOfLines    = 0;
        
        [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo (15);
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(weakSelf.accessoryImageView.mas_leading).with.offset(-10);
            make.height.mas_equalTo(20);
        }];
        
        [self.image makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSelf.subLabel.mas_centerY);
            make.leading.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(11.5, 15));
        }];
        
        [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(10);
            make.leading.mas_equalTo(weakSelf.image.mas_trailing).with.offset(10);
            make.trailing.mas_equalTo(-10);
            make.bottom.mas_equalTo(-10);
        }];
    }

    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
    
    [self.accessoryImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.titleLabel.mas_centerY);
        make.trailing.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(6, 12));
    }];
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor   = UIColorFromRGB(0x333333, 1);
        _titleLabel.font        = [UIFont systemFontOfSize:15];
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            _titleLabel.textAlignment = NSTextAlignmentRight ;
        }
        else {
            _titleLabel.textAlignment = NSTextAlignmentLeft;
        }
    }
    
    return _titleLabel;
}

- (UILabel *)subLabel {
    
    if (_subLabel == nil) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.textColor        = UIColorFromRGB(0x333333, 1);
        _subLabel.font             = [UIFont systemFontOfSize:15];
    }
    
    return _subLabel;
}

- (UIImageView *)image {

    if (_image == nil) {
        _image = [[UIImageView alloc] init];
        _image.image = [UIImage imageNamed:@"lz_userinfo_location"];
    }
    
    return _image;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (UIImageView *)accessoryImageView {
    
    if (_accessoryImageView == nil) {
        _accessoryImageView = [[UIImageView alloc] init];
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_ar"];
        } else {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_en"];
        }
    }
    
    return _accessoryImageView;
}

+ (CGFloat)getCellSizeWithText:(NSString *)text {
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.]};
    CGRect titleRect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-55, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:attributes
                                          context:nil];
    return titleRect.size.height;
}

- (void)setModel:(LZMerchantInfoModel *)model {
    
    _model = model;
    
    switch (self.tag) {
        case 0: {
            self.subLabel.text = _model.restaurantName;
            self.titleLabel.text = LZLocalizedString(@"restaurant_name", nil);
            self.accessoryImageView.hidden = YES;
        }
            break;
        case 1: {
            self.subLabel.text = _model.cookingStyle;
            self.titleLabel.text = LZLocalizedString(@"cuisine", nil);
            self.accessoryImageView.hidden = NO;
        }
            break;
        case 2: {
            self.subLabel.text = _model.restaurantAddress;
            self.titleLabel.text = LZLocalizedString(@"restaurant_address", nil);
            self.accessoryImageView.hidden = YES;
        }
            break;
        case 3: {
            self.subLabel.text = _model.mapLocationAddress;
            self.titleLabel.text = LZLocalizedString(@"map_location", nil);
            self.accessoryImageView.hidden = NO;
        }
            break;
        case 4: {
            self.subLabel.text = _model.cellphone;
            self.titleLabel.text = LZLocalizedString(@"phone", nil);
            self.accessoryImageView.hidden = YES;
        }
            break;
            
        default:
            break;
    }
}

@end
